<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Install extends MY_AdminController
{
    function __construct()
    {
        parent::__construct();
    }

    function Index()
    {
        $this->load->helper('security');
        $username = 'Administrator';
        $password = '123456';
        $email = 'Administrator@gmail.com';
        $password = do_hash($password);
        $this->load->database();
        $this->load->dbforge();

        /*---------------Create Session Table-----------------------*/
        $fields = array(
            'session_id' => array(
                'type' => 'VARCHAR',
                'constraint' => '40',
                'default' => '0',
            ),
            'ip_address' => array(
                'type' => 'VARCHAR',
                'constraint' => '45',
                'default' => '0',
            ),
            'user_agent' => array(
                'type' => 'VARCHAR',
                'constraint' => '120',
            ),
            'last_activity' => array(
                'type' => 'INT',
                'constraint' => '10',
                'unsigned' => TRUE,
                'default' => '0',
            ),
            'user_data' => array(
                'type' => 'TEXT'
            )
        );
        $this->dbforge->add_field($fields);
        $this->dbforge->add_key('session_id', TRUE);
        $this->dbforge->add_key('last_activity');
        $this->dbforge->create_table($this->config->item('sess_table_name'), TRUE);

        /*---------------Create Power Table-----------------------*/
        $this->dbforge->add_field($this->map->power()->field());
        foreach ($this->map->power()->primaryKey() as $key) {
            $this->dbforge->add_key($key, TRUE);
        }
        foreach ($this->map->power()->key() as $key) {
            $this->dbforge->add_key($key);
        }
        $this->dbforge->create_table($this->map->power, TRUE);
        if ($this->db->get_where($this->map->power, array($this->map->power()->ID => 1))->num_rows() == 0) {
            $this->db->insert($this->map->power, array(
                $this->map->power()->ID => 1,
                $this->map->power()->Name => 'Administrator',
                $this->map->power()->Description => 'Development Account',
                $this->map->power()->Created => time(),
                $this->map->power()->UserID => 1,
                $this->map->power()->Activated => 1,
                $this->map->power()->Deleted => 0
            ));
        }

        /*---------------Create User Table-----------------------*/
        $this->dbforge->add_field($this->map->user()->field());
        foreach ($this->map->user()->primaryKey() as $key) {
            $this->dbforge->add_key($key, TRUE);
        }
        foreach ($this->map->user()->key() as $key) {
            $this->dbforge->add_key($key);
        }
        $this->dbforge->create_table($this->map->user, TRUE);
        if ($this->db->get_where($this->map->user, array($this->map->user()->ID => 1))->num_rows() == 0) {
            $this->db->insert($this->map->user, array(
                $this->map->user()->ID => 1,
                $this->map->user()->Username => $username,
                $this->map->user()->Password => $password,
                $this->map->user()->Email => $email,
                $this->map->user()->PowerID => 1,
                $this->map->user()->Key => '',
                $this->map->user()->Created => time(),
                $this->map->user()->UserID => 1,
                $this->map->user()->Activated => 1,
                $this->map->user()->Deleted => 0
            ));
        }

        /*---------------Create Type Input Table-----------------------*/
        $this->dbforge->add_field($this->map->type_input()->field());
        foreach ($this->map->type_input()->primaryKey() as $key) {
            $this->dbforge->add_key($key, TRUE);
        }
        foreach ($this->map->type_input()->key() as $key) {
            $this->dbforge->add_key($key);
        }
        $this->dbforge->create_table($this->map->type_input, TRUE);
        if ($this->db->get_where($this->map->type_input, array($this->map->type_input()->ID => 1))->num_rows() == 0) {
            $this->db->insert($this->map->type_input, array(
                $this->map->type_input()->ID => 1,
                $this->map->type_input()->Name => 'Text Field',
                $this->map->type_input()->Description => 'Text Field',
                $this->map->type_input()->Created => time(),
                $this->map->type_input()->UserID => 1,
                $this->map->type_input()->Activated => 1,
                $this->map->type_input()->Deleted => 0
            ));
        }

        /*---------------Create Type Output Table-----------------------*/
        $this->dbforge->add_field($this->map->type_output()->field());
        foreach ($this->map->type_output()->primaryKey() as $key) {
            $this->dbforge->add_key($key, TRUE);
        }
        foreach ($this->map->type_output()->key() as $key) {
            $this->dbforge->add_key($key);
        }
        $this->dbforge->create_table($this->map->type_output, TRUE);
        if ($this->db->get_where($this->map->type_output, array($this->map->type_output()->ID => 1))->num_rows() == 0) {
            $this->db->insert($this->map->type_output, array(
                $this->map->type_output()->ID => 1,
                $this->map->type_output()->Name => 'Text Field',
                $this->map->type_output()->Description => 'Text Field',
                $this->map->type_output()->Created => time(),
                $this->map->type_output()->UserID => 1,
                $this->map->type_output()->Activated => 1,
                $this->map->type_output()->Deleted => 0
            ));
        }

        /*---------------Create Field Table-----------------------*/
        $this->dbforge->add_field($this->map->field()->field());
        foreach ($this->map->field()->primaryKey() as $key) {
            $this->dbforge->add_key($key, TRUE);
        }
        foreach ($this->map->field()->key() as $key) {
            $this->dbforge->add_key($key);
        }
        $this->dbforge->create_table($this->map->field, TRUE);

        /*---------------Create Table Table-----------------------*/
        $this->dbforge->add_field($this->map->table()->field());
        foreach ($this->map->table()->primaryKey() as $key) {
            $this->dbforge->add_key($key, TRUE);
        }
        foreach ($this->map->table()->key() as $key) {
            $this->dbforge->add_key($key);
        }
        $this->dbforge->create_table($this->map->table, TRUE);

        /*----------------------THE--END--------------------------*/
        $this->load->model(MY_AdminController::MODEL_FOLDER . 'modelDatabase');
        $this->modelDatabase->sync();
    }
}
